﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{

    public class MerAccountController : Controller
    {
        //
        // GET: /MerAccount/

        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        #region  获取当前操作员拥有商户的权限
        /// <summary>
        /// 获取当前操作员拥有商户的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = wsdcser.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    list = ParseClass<Model.Order_Mer_T>.JsonList(jieguo);
                    //if (jieguo.Count > 0)
                    //{
                    //    foreach (JToken item in jieguo)
                    //    {
                    //        Model.Order_Mer_T m = new Model.Order_Mer_T();
                    //        m.MerID = item["MerID"].ToString();
                    //        m.MerName = item["MerName"].ToString();
                    //        m.MerAdr = item["MerAdr"].ToString();
                    //        m.MerCall = item["MerCall"].ToString();
                    //        m.MerTxt = item["MerTxt"].ToString();
                    //        list.Add(m);
                    //    }
                    //}
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {              
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 根据商户ID获取账目
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="jqgridparam">datagrid页面参数</param>
        /// <returns></returns>
        public ActionResult getgrid(string MerID, string MerAccType, string MerAccDate, JqGridParam jqgridparam)
        {
            try
            {
                MerAccDate = MerAccDate.Replace("-", "");
                Page<Model.View_Order_MerAccount> page = new Page<Model.View_Order_MerAccount>();//分页类定义
                List<Model.View_Order_MerAccount> list = new List<Model.View_Order_MerAccount>();//定义商品分类设置
                string result = wsdcser.GetOrder_MerAccount(jqgridparam.page,jqgridparam.rows, MerID,MerAccType,MerAccDate, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    list = ParseClass<Model.View_Order_MerAccount>.JsonList(jieguo);
                    foreach (View_Order_MerAccount m in list)
                    {
                        if (m.MerAccInt != "" & m.MerAccInt != null)
                        {
                            m.MerAccInt = DataHelper.MoneyToYuang(m.MerAccInt.ToString());
                        }
                        if (m.MerAccOut != "" & m.MerAccOut!=null)
                        {
                            m.MerAccOut = DataHelper.MoneyToYuang(m.MerAccOut.ToString());
                        }
                    }
                    //jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else if (bs == "0")
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    return Json(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
        #endregion

        #region 根据商户ID获取商户账目
        /// <summary>
        /// 获取对应商户账目
        /// </summary>
        /// <returns></returns>
        public List<View_Order_MerAccount> GetAllAcount(string MerID)
        {
            List<View_Order_MerAccount> list = new List<View_Order_MerAccount>();
            string result = wsdcser.GetOrder_MerAccount(1, 20, MerID,"","", GetYZM._YZM, 1);
            JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
            string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
            if (bs == "1")//返回结果正常
            {
                IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                if (jieguo.Count > 0)
                {
                    //list = ParseClass<View_Order_MerAccount>.JsonList(jieguo);
                    foreach (JToken item in jieguo)
                    {
                        View_Order_MerAccount m = new View_Order_MerAccount();
                        m.MerAccBZ = item["MerAccBZ"].ToString();
                        m.MerAccDate = int.Parse(item["MerAccDate"].ToString());
                        m.MerAccID = int.Parse(item["MerAccID"].ToString());
                        if (item["MerAccInt"].ToString() != "" & item["MerAccInt"].ToString()!= null)
                        {
                            m.MerAccInt = DataHelper.MoneyToYuang(item["MerAccInt"].ToString());
                        }
                        if (item["MerAccOut"].ToString() != "" & item["MerAccOut"].ToString() != null)
                        {
                            m.MerAccOut = DataHelper.MoneyToYuang(item["MerAccOut"].ToString());
                        }
                        m.MerAccSate = int.Parse(item["MerAccSate"].ToString());
                        m.MerAccTime = int.Parse(item["MerAccTime"].ToString());
                        m.MerAccType = int.Parse(item["MerAccType"].ToString());
                        m.MerID = item["MerID"].ToString();
                        m.MerName = item["MerName"].ToString();
                        list.Add(m);
                    }
                }
                return list;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 根据账目类型、日期查询账目
        /// <summary>
        /// 根据账目类型、日期查询账目
        /// </summary>
        /// <param name="MerAccType"></param>
        /// <param name="MerAccDate"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult SearchAccount(string MerID, int MerAccType, string MerAccDate, JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.View_Order_MerAccount> page = new Page<Model.View_Order_MerAccount>();//
                List<View_Order_MerAccount> ls = GetAllAcount(MerID);
                List<Model.View_Order_MerAccount> newls = new List<Model.View_Order_MerAccount>();// 
                //MerAccDate = MerAccDate.Replace("-", "");
                //int d = int.Parse(MerAccDate);
                if (MerAccType != -1 && MerAccDate == "")
                {
                    ls = ls.Where(i => i.MerAccType == MerAccType).ToList();
                }
                else if (MerAccDate != "" && MerAccType == -1)
                {
                    MerAccDate = MerAccDate.Replace("-", "");
                    int d = int.Parse(MerAccDate);
                    ls = ls.Where(i => i.MerAccDate == d).ToList();
                }
                else if (MerAccType != -1 && MerAccDate != "")
                {
                    MerAccDate = MerAccDate.Replace("-", "");
                    int d = int.Parse(MerAccDate);
                    ls = ls.Where(i => i.MerAccDate == d && i.MerAccType == MerAccType).ToList();
                }
                jqgridparam.records = ls.Count;//查询的数据总数
                var data = new
                {
                    total = ls.Count,
                    rows = page.FindListPage(ls, ref jqgridparam)//返回当前页的数据 
                };
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region 获取当前页数据
        /// <summary>
        /// 获取当前页数据
        /// </summary>
        /// <param name="list">总的list数据</param>
        /// <param name="jqgridparam">分页参数</param>
        /// <returns></returns>
        public List<Model.View_Order_MerAccount> FindListPage(List<Model.View_Order_MerAccount> list, ref JqGridParam jqgridparam)
        {
            try
            {
                List<Model.View_Order_MerAccount> RetList = new List<Model.View_Order_MerAccount>();
                string orderField = jqgridparam.sort;//排序列
                string orderType = jqgridparam.order;//排序方式
                int pageIndex = jqgridparam.page;//当前页
                int pageSize = jqgridparam.rows;//每页行数
                int totalRow = jqgridparam.records;//总记录数
                int lastIndex = pageIndex * pageSize;//当前页最后一行
                if (orderType == "asc")
                {
                    list = (from p in list orderby orderField ascending select p).ToList();
                }
                else if ((orderType == "desc"))
                {
                    list = (from p in list orderby orderField descending select p).ToList();
                }
                //如果总行数小于当前页最后一行时 当前页最后一行等于总行数
                if (totalRow < lastIndex)
                {
                    lastIndex = totalRow;
                }
                int fistIndex = (pageIndex - 1) * pageSize;
                int j = 0;
                for (int i = fistIndex; i < lastIndex; i++)
                {
                    j++;
                    RetList.Add(list[i]);
                }
                return RetList;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion
    }
}
